var express = require('express');
var router = express.Router();

var { selectAdmin } = require("../db/login.js")

var { getNormalCap, getNumCap } = require("../utils/identifyCode")

router.get('/', function (req, res, next) {
	res.send('respond with a resource');
});

router.post('/login', function (req, res, next) {
	let body = req.body;
	let username = body.username;
	let password = body.password;
	let data = {}
	selectAdmin(username).then(e => {
		if (e.length && e[0].mangerPwd == password) {
			data.message = "登录成功"
			data.code = "0"
		} else {
			data.message = "用户名或密码错误"
			data.code = "1"
		}
		res.send(data)
	}).catch(e => {
		data.message = "系统出错"
		data.code = "err"
		data.data = e
		res.send(data)
	})
})

router.get('/getCode', function (req, res, next) {
	console.log("ok")
	// let cap = getNormalCap()
	let cap = getNumCap()
	res.send(cap)
});

module.exports = router;
